package com.coderdream.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 1、按字节读取文件内容 <br>
 * 2、按字符读取文件内容 <br>
 * 3、按行读取文件内容 <br>
 * 4、随机读取文件内容
 * 
 * @author xulin
 * 
 */
public class HistoryUtil {

	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static Map<String, String> getHistoryList(Set<String> set,
			String fileName) {
		Map<String, String> map = new TreeMap<String, String>();
		List<String> list = ReadFromFile.readFileToList(fileName);
		for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
			//String string = iterator.next();

		}

		return map;
	}

	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static List<String> readFileToSQLStringList(String fileName) {
		List<String> list = new ArrayList<String>();
		// 得到classes的路径
		String url = ReadFromFile.class.getClassLoader().getResource("")
				.getPath();
		// System.out.println(url);
		// 然后再加上文件的路径
		File file = new File(url + fileName);
		BufferedReader reader = null;
		try {
			// System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			// 一次读入一行，直到读入null为文件结束
			String day = "";
			while ((tempString = reader.readLine()) != null) {
				if (null != tempString && !"".equals(tempString)) {
					int index1 = tempString.indexOf("月");
					int index2 = tempString.indexOf("·");

					if (-1 != index1 && -1 == index2) {
						day = tempString;

						// System.out.println(day);
					} else {
						// System.out.println(tempString.trim());
						// String str = day + ":" + tempString;
						// System.out.println(str);
						// INSERT INTO Persons (LastName, Address) VALUES
						// ('Wilson', 'Champs-Elysees')
						String sql = "INSERT INTO HISTORY(day,event) VALUES('"
								+ DateUtil.transferDayToNumberString(day)
								+ "','"
								+ tempString.substring(1, tempString.length())
								+ "');";
						System.out.println(sql);
						list.add(sql);
					}
				}
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}

		return list;
	}

	public static void main(String[] args) {
		readFileToSQLStringList("history2.txt");
	}
}
